#include <csignal>
#include <thread>
#include <chrono>
#include "MqttClient.hpp"

bool running = true;
void signalHandler(int) { running = false; }

class MyDeviceMqtt : public MqttClient {
public:
    using MqttClient::MqttClient;

    void onConnect(int rc) override {
        if (rc == 0) {
            std::cout << "[MyDeviceMqtt] 已成功连接\n";
            subscribe("/cmd/test");
        }
    }

    void onMessage(const std::string &topic, const std::string &payload) override {
        std::cout << "[MyDeviceMqtt] 收到消息: " << payload << "\n";
        publish("/data/reply", "{\"msg\": \"Hello from MyDevice!\"}");
    }
};

int main() {
    signal(SIGINT, signalHandler);

    MyDeviceMqtt client("2K300", "47.109.28.214", 1883, "root", "$Yang20050701");
    if (!client.connect()) return -1;

    while (running) {
        client.publish("/status/deviceON", "{\"status\":\"ONLINE\"}");// 发送心跳
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }

    return 0;
}
